<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\index\model\Users;
use app\index\model\Activity;
use app\index\model\Complaints;
use app\index\model\Blacklist;
class User extends Common
{
	//Ta的资料信息详情
    public function userDetails()
    {
        $uid = Request::instance()->post('uid','');//用户id
        $nearby_id = Request::instance()->post('nearby_id','');//偶遇用户id
         if(empty($nearby_id)){
           return json(['state'=>'204','msg'=>'参数不足']);
         }
        if(!empty($uid)){
           $rs=Users::selOne($uid);
           if(empty($rs)){
              return json(['state'=>'202','msg'=>'用户已不存在']);
           }
           $lat=$rs['latitude'];//纬度
           $lng=$rs['longitude'];//经度
           $result=Users::userSel($nearby_id);
           if(empty($result)){
            return json(['state'=>'201','msg'=>'查询内容不存在']);
           }
           //查询Ta被投诉的记录
          $complaint=Db::name('complaint')
             ->field('content,createTime')
             ->where(['nearby_id'=>$nearby_id,'state'=>'1'])
             ->order('createTime desc')
             ->limit(3)
             ->select();
          //判断我关注过
           $relation=Db::name('user_relation')
              ->field('uid,follow_uid,status')
             ->where(['uid'=>$uid,'follow_uid'=>$nearby_id])
             ->find(); 
           //判读关注过没  
           if($relation){
            $relation1='1';
           }else{
            $relation1='-1';
           }  
        //根据是否相互聊天
        //查询两个人是否参加任意一场相同的活动也可聊天
       // $uids="$uid,$nearby_id";
       $rest1 =Db::name('activity_users')->where('uids',$uid)->column('activity_id');
       $rest2 =Db::name('activity_users')->where('uids',$nearby_id)->column('activity_id');
         $rest=array_intersect($rest1,$rest2); //数组交集
         
         //查询我是否被拉黑
          $red=Db::name('user_blacklist')->where(['blacklist_id'=>$uid,'uid'=>$nearby_id])->find();
          //查询我是不是把对方拉黑
         $reds= Db::name('user_blacklist')->where(['uid'=>$uid,'blacklist_id'=>$nearby_id])->find();
         if(empty($red) && empty($reds)){
          if($relation['status']==1 || !empty($rest)){
            $chatId ='1';
           }else{
            $chatId='-1';
           } 
         }else{
          $chatId='-1';
         }
         
        //判断Ta是不是在我的黑名单里
       $blacklist=Db::name('user_blacklist')->where(['uid'=>$uid,'blacklist_id'=>$nearby_id])->find();
         if(!empty($blacklist)){
           $blackId='1';
         }else{
          $blackId='-1';
         }       
        $result['complaint']=$complaint;     
        $result['blackId']=$blackId;     
        $result['chatId']=$chatId;     
        $result['stateId']=$relation1;    
        //print_r($result);die; 
        $result['distance']=$this->getDistance($lng, $lat, $result['longitude'], $result['latitude'], 2);
        }else{
            $result=Users::userSel($nearby_id);
        }
        if($result){
        	return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
        }else{
        	return json(['state'=>'201','msg'=>'查询内容不存在']);
        }  	
}
//Ta参加的活动
public function activity_adverse(){
  $nearby_id=input('nearby_id');
  $page=input('page');//页码值
  if(empty($nearby_id) || empty($page)){
    return json(['state'=>'202','msg'=>'参数不足']);
  }
   $limit =3;
   $start = ($page-1)*$limit;
   $result=Db::name('activity_users')->where(['uids'=>$nearby_id])->limit($start,$limit)->select();
   if($result){
     $activity=array_column($result,'activity_id'); 
           $activity_id=implode(',',$activity);       
         //获取参加的活动详情
         $activityList=Db::name('activity')
                   ->where('id','in',$activity_id)
                   ->field('id as activity_id,name,time_from,time_to,location,goodsThums')
                   ->select();
    return json(['state'=>'200','msg'=>'查询成功','result'=>$activityList]);
   }else{
    return json(['state'=>'200','msg'=>'查询成功','result'=>$result]);
   }

}
/*
活动劵接口
 */
public function activitieSecurit(){
   $uid = Request::instance()->post('uid','');//用户id
      if(empty($uid)){
          return json(['state'=>'203','msg'=>'用户为空']);
        }
       $rs=Users::selOne($uid);
       if($rs){
         $result=Activity::discounts();
         if($result){
             return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
         }else{
            return json(['state'=>'201','msg'=>'查询内容不存在']); 
         }
       }else{
         return json(['state'=>'202','msg'=>'用户已不存在']);
       }
}
//领取活动劵
public function securitDraw(){
   $uid = Request::instance()->post('uid','');//用户id
   $securitId = Request::instance()->post('securitId','');//活动劵id
      if(empty($uid) || empty($securitId)){
          return json(['state'=>'203','msg'=>'参数不足']);
        }
       $rs=Users::selOne($uid);
       if($rs){
        $data=['uid'=>$uid,'securitId'=>$securitId,'create_time'=>date("Y-m-d H:i:s")];
         $result=Activity::discounts_add($data);
         if($result){
             return json($this->array_nonull(['state'=>'200','msg'=>'领取成功']));
         }else{
            return json(['state'=>'201','msg'=>'领取失败']); 
         }
       }else{
         return json(['state'=>'202','msg'=>'用户已不存在']);
       }
}
//我参加的活动
  public function MyActivity(){
  	 $uid = Request::instance()->post('uid','');//用户id
     $page=input('page');//获取页码值
  	  if(empty($uid)){
          return json(['state'=>'203','msg'=>'用户为空']);
        }
        if(empty($page)){
          return json(['state'=>'204','msg'=>'页码值为空']);
        }
       $rs=Users::selOne($uid);
       if($rs){
         $limit =3;
         $start = ($page-1)*$limit;
         $result=Activity::Myactivity($uid,$start,$limit);
         if($result){
             return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
         }else{
            return json(['state'=>'201','msg'=>'查询内容不存在']);	
         }
       }else{
         return json(['state'=>'202','msg'=>'用户已不存在']);
       }
  }
  //活动评价
  public function appraise(){
    $uid=Request::instance()->post('uid');//用户id
    $activity_id=Request::instance()->post('activity_id');//活动id
    $grade=(float)input('grade');//活动评分
    $content=Request::instance()->post('content');//评价内容
     if(empty($uid)){
          return json(['state'=>'202','msg'=>'搜索内容为空']);
        }
      if(empty($activity_id) || empty($content) || empty($content)){
         return json(['state'=>'203','msg'=>'参数不全']);
      }  
       $res=Users::selOne($uid);
       if($res){
         $rs=Activity::reviewSel($uid,$activity_id);
         if($rs){
           return json(['state'=>'205','msg'=>'活动已经评价过啦']);  
         }else{
            //查询领队
            $captain=Db::name('activity')->where(['id'=>$activity_id])->find();
           $data=['uid'=>$uid,'activity_id'=>$activity_id,'grade'=>$grade,'content'=>$content,'captain_id'=>$captain['captain_id']];
            $result=Activity::reviewSet($data);
           if($result){
             //查询活动的评分
            $appraise= Db::name('activity_appraise')->where(['activity_id'=>$activity_id])->field('Sum(grade) as grade')->select();
            if(!empty($appraise[0]['grade'])){
              //查询评分人数
            $num=Db::name('activity_appraise')->where(['activity_id'=>$activity_id])->count();
            //计算平均分
            $rate=$appraise[0]['grade']/$num;
            //修改领队的评分
            $sdata1=['rate'=>$rate,'update_time'=>date("Y-m-d H:i:s")];
             Db::name('user_info')->where(['uid'=>$captain['captain_id'],'member_type'=>'2'])->update($sdata1);
            }
             return json($this->array_nonull(['state'=>'200','msg'=>'评价成功','result'=>$result]));
           }else{
            return json(['state'=>'201','msg'=>'评价失败再试试']);  
             }
          }
       }else{
            return json(['state'=>'204','msg'=>'用户已不存在']);  
       }
         
  }
  //会员搜索
  public function userMember(){
      $nickname = Request::instance()->post('nickname','');//用户id
      if(empty($nickname)){
          return json(['state'=>'202','msg'=>'搜索内容为空']);
        }
       $result=Users::member($nickname);
         if($result){
             return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
         }else{
            return json(['state'=>'201','msg'=>'查询内容不存在']);  
         }
  }
  //领队搜索
  public function team(){
     $captainName = Request::instance()->post('captainName','');//领队名称
      if(empty($captainName)){
          return json(['state'=>'202','msg'=>'搜索内容为空']);
        }
       $result=Users::teamSet($captainName);
         if($result){
             return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
         }else{
            return json(['state'=>'201','msg'=>'查询内容不存在']);  
         }
  }
  //我的活动搜索
  public function userActivity(){
     $activityName = Request::instance()->post('activityName','');//领队名称
      if(empty($activityName)){
          return json(['state'=>'202','msg'=>'搜索内容为空']);
        }
       $result=Activity::activitySet($activityName);
         if($result){
             return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));
         }else{
            return json(['state'=>'201','msg'=>'查询内容不存在']);  
         }
  }
  //投诉个人接口
  public function complaintAdd(){
      $uid = Request::instance()->post('uid','');//用户id
      $nearby_id = Request::instance()->post('nearby_id','');//投诉者id
      $cause = Request::instance()->post('cause','');//投诉原因
      $content = Request::instance()->post('content','');//投诉内容
      if(!empty($uid) && !empty($cause) && !empty($content) && !empty($nearby_id)){
        $rs=Users::selOne($uid);
        if($rs){
          //判断投诉审核状态
          $re=Complaints::ComplaintSel($uid);
          if(!empty($re)){
              return json(['state'=>'204','msg'=>'请等待上个投诉审核']); 
            }else{
               $rd=Complaints::repetitionSel($uid,$nearby_id);
               if(!empty($rd)){
                   return json(['state'=>'203','msg'=>'您的投诉不可重复提交']); 
               }else{
                   $data=['uid'=>$uid,'nearby_id'=>$nearby_id,'cause'=>$cause,'content'=>$content,'createTime'=>date("Y-m-d H:i:s")];
                  $result=Complaints::ComplaintSet($data);
                  if($result){
                    return json(['state'=>'200','msg'=>'投诉成功等待审核']);
                  }else{
                    return json(['state'=>'201','msg'=>'投诉失败再试试']);
                  }
            } 
          }
        }else{
          return json(['state'=>'205','msg'=>'用户已不存在']);  
        }
      }else{
           return json(['state'=>'206','msg'=>'参数不足']);  
      }
  } 
  //加入黑名单
  public function blacklistAdd(){
      $uid = Request::instance()->post('uid','');//用户id
      $nearby_id = Request::instance()->post('nearby_id','');//Ta的id
      if(!empty($uid) && !empty($nearby_id)){
        $re=Blacklist::sel($uid,$nearby_id);
        if($re){
         return json(['state'=>'203','msg'=>'已经加入啦黑名单']); 
        }else{
        	$data=['uid'=>$uid,'blacklist_id'=>$nearby_id];
        	$result=Blacklist::BlacklistAdd($data);
        	if($result){
            $res=1;
        		return json(['state'=>'200','msg'=>'加入成功','result'=>$res]); 	
        	}else{
        		return json(['state'=>'201','msg'=>'加入失败再试试']); 	
        	}
        }
      }else{
      	 return json(['state'=>'202','msg'=>'参数不足']); 
      }
  }
  //我的黑名单
  public function blacklistShow(){
  	$uid=Request::instance()->post('uid','');//用户id
  	if(empty($uid)){
  		return json(['state'=>'203','msg'=>'请求的用户为空']); 	
  	}
  	$rs=users::selOne($uid);
  	if($rs){
  		 $result=Blacklist::BlacklistSel($uid);
  	 if($result){
         return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result])); 	
  	 }else{
  	    return json(['state'=>'201','msg'=>'还没有黑名单人员']); 	
  	 }
  	}else{
  	  return json(['state'=>'202','msg'=>'用户已不存在']); 	
  	}
  	
  }
  //取消黑名单人员
  public function user_remove(){
  	$uid=(int)input('uid','');//用户id
  	$blacklist_id=(int)input('blacklist_id','');//黑名单人员的id
  	if(empty($uid) || empty($blacklist_id)){
  		return json(['state'=>'203','msg'=>'参数不足']); 	
  	}
  	$rs=users::selOne($uid);
  	if($rs){
  		 $result=Blacklist::blacklistDel($uid,$blacklist_id);
  	 if($result){
        $sdata='-1';
         return json($this->array_nonull(['state'=>'200','msg'=>'移除成功','result'=>$sdata])); 	
  	 }else{
  	    return json(['state'=>'201','msg'=>'移除失败再试试']); 	
  	 }
  	}else{
  	  return json(['state'=>'202','msg'=>'用户已不存在']); 	
  	}
  }
  //注销账号
  public function forbidden(){
  	$uid=(int)input('uid','');//用户id
  	$name=input('name','');//原因
  	$content=input('content','');//内容
  	if(empty($uid) || empty($name)){
  		return json(['state'=>'203','msg'=>'参数不足']); 	
  	}
  	$rs=users::selOne($uid);
  	if($rs){
  		 $rd=users::forbiddenSel($uid);
  		 if($rd){
            return json(['state'=>'204','msg'=>'您已经提交过了请等待审核']); 	
  		 }else{
  		 	$data=['uid'=>$uid,'name'=>$name,'content'=>$content,'create_time'=>date("Y-m-d H:i:s")];
  		 	$result=users::forbiddenAdd($data);
		  	 if($result){
		         return json($this->array_nonull(['state'=>'200','msg'=>'提交成功请等待审核'])); 	
		  	 }else{
		  	    return json(['state'=>'201','msg'=>'提交失败再试试']); 	
		  	 }
  		 }
  		 
  	}else{
  	  return json(['state'=>'202','msg'=>'用户已不存在']); 	
  	}
  }
  //设置交友状态接口
  public function pelSet(){
    $uid=input('uid');//用户id
    $userFlag=input('userFlag');//1开启 -1关闭
    if(empty($uid) || empty($userFlag)){
      return json(['state'=>'202','msg'=>'参数不足']);  
    }
  }
  //通知设置
  public function myNotify(){
  	$uid=(int)input('uid','');//用户id
  	$concernId=(int)input('concernId','');//关注状态通知 1通知 -1不通知
  	$actnotify=(int)input('actnotify','');//黑关注的人新活动通知 1通知 -1不通知
  	$captain_state=(int)input('captain_state','');//关注的领队新活动通知 1通知 -1不通知
  	if(!empty($uid) && !empty($concernId) && !empty($actnotify) && !empty($captain_state)){
  		$rs=users::selOne($uid);
  	if($rs){
  		$data=['concernId'=>$concernId,'actnotify'=>$actnotify,'captain_state'=>$captain_state];
  		 $result=Users::inform($uid,$data);
  	 if($result){
         return json($this->array_nonull(['state'=>'200','msg'=>'设置成功'])); 	
  	 }else{
  	    return json(['state'=>'201','msg'=>'设置失败再试试']); 	
  	 }
  	 }else{
  	  return json(['state'=>'202','msg'=>'用户已不存在']); 	
  	}
  	}else{
  		return json(['state'=>'203','msg'=>'参数不足']); 	
  	}
  }
  //消息通知列表接口
   public function myNews(){
    $uid=(int)input('uid','');//用户id
    if(empty($uid)){
      return json(['state'=>'202','msg'=>'参数为空']);
      }
      $rs=users::selOne($uid);
     if(empty($rs)){
      return json(['state'=>'203','msg'=>'用户已不存在']);
     }
      $result=Db::name('user_info')->where(['uid'=>$uid,'userFlag'=>'1'])->field('concernId,actnotify,captain_state')->find();
     if($result){
         return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));  
     }else{
        return json(['state'=>'201','msg'=>'查询内容不存在']);   
     }
    
  }
  //我的接口
  public function myMessage(){
    $uid=(int)input('uid','');//用户id
    if(empty($uid)){
      return json(['state'=>'202','msg'=>'参数为空']);
     }
     $rs=users::selOne($uid);
     if(empty($rs)){
      return json(['state'=>'203','msg'=>'用户已不存在']);
     }
     $result=Db::name('user_info')->where(['uid'=>$uid,'userFlag'=>'1'])->field('uid,userPhoto,nickname,attention,fans,focusNum as stockNum')->find();
     //查询我参加的活动数
      $actNum=Db::name('activity_users')->where(['uids'=>$uid])->count();
      if(empty($actNum)){
        $actNum=0;
      }
     $result['actNum']=$actNum;
     if($result){
         return json($this->array_nonull(['state'=>'200','msg'=>'查询成功','result'=>$result]));  
     }else{
        return json(['state'=>'201','msg'=>'查询内容不存在']);   
     }
  }

}